
#!/bin/bash


EXTERNALS_DIRECTORY=`pwd`/externals
SOURCE_DIR=${EXTERNALS_DIRECTORY}

# =================================Install lua===================================
LIB_VERSION=5.4.4
LIB_NAME=lua-${LIB_VERSION}
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}
LUA_ROOT=${LIB_INSTALL_DIR}

if [ ! -d ${LIB_INSTALL_DIR} ]; then
	cmake -S ${LIB_SRC_DIR} \
		-B ${LIB_BUILD_DIR} \
		-DCMAKE_BUILD_TYPE=Release \
		-DBUILD_SHARED_LIBS=OFF \
		-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} \
		-DCMAKE_INSTALL_PREFIX=${LIB_INSTALL_DIR} \
		-DANDROID_ABI=${ANDROID_ABI} \
		-DANDROID_PLATFORM=${ANDROID_PLATFORM}
	cmake --build ${LIB_BUILD_DIR}
	cmake --install ${LIB_BUILD_DIR}
fi

# =================================Install libuv===================================
LIB_VERSION=1.44.1
LIB_NAME=libuv-${LIB_VERSION}
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}
LIBUV_ROOT=${LIB_INSTALL_DIR}

if [ ! -d ${LIB_INSTALL_DIR} ]; then
	cmake -S ${LIB_SRC_DIR} \
		-B ${LIB_BUILD_DIR} \
		-DCMAKE_BUILD_TYPE=Release \
		-DBUILD_SHARED_LIBS=OFF \
		-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} \
		-DCMAKE_INSTALL_PREFIX=${LIB_INSTALL_DIR} \
		-DANDROID_ABI=${ANDROID_ABI} \
		-DANDROID_PLATFORM=${ANDROID_PLATFORM}
	cmake --build ${LIB_BUILD_DIR}
	cmake --install ${LIB_BUILD_DIR}
fi

# =================================Install luv===================================
LIB_VERSION=1.44.2
LIB_NAME=luv-${LIB_VERSION}
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}
LUV_ROOT=${LIB_INSTALL_DIR}

#rm -fr ${LIB_INSTALL_DIR}
if [ ! -d ${LIB_INSTALL_DIR} ]; then
	cmake -S ${LIB_SRC_DIR} \
		-B ${LIB_BUILD_DIR} \
		-DLUA_INSTALL_DIRECTORY=${LUA_ROOT} \
		-DLIBUV_INSTALL_DIRECTORY=${LIBUV_ROOT} \
		-DCMAKE_BUILD_TYPE=Release \
		-DBUILD_SHARED_LIBS=OFF \
		-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} \
		-DCMAKE_INSTALL_PREFIX=${LIB_INSTALL_DIR} \
		-DANDROID_ABI=${ANDROID_ABI} \
		-DANDROID_PLATFORM=${ANDROID_PLATFORM}
	cmake --build ${LIB_BUILD_DIR}
	cmake --install ${LIB_BUILD_DIR}
fi

# =================================Install curl===================================
LIB_VERSION=7.82.0
LIB_NAME=curl-${LIB_VERSION}
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}
CURL_ROOT=${LIB_INSTALL_DIR}

if [ ! -d ${LIB_INSTALL_DIR} ]; then
	cmake -S ${LIB_SRC_DIR} \
		-B ${LIB_BUILD_DIR} \
		-DCMAKE_BUILD_TYPE=Release \
		-DBUILD_SHARED_LIBS=OFF \
		-DCURL_ENABLE_SSL=OFF \
		-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} \
		-DCMAKE_INSTALL_PREFIX=${LIB_INSTALL_DIR} \
		-DANDROID_ABI=${ANDROID_ABI} \
		-DANDROID_PLATFORM=${ANDROID_PLATFORM}
	cmake --build ${LIB_BUILD_DIR}
	cmake --install ${LIB_BUILD_DIR}
fi

# =================================Install lua-curl===================================
LIB_VERSION=0.3.13
LIB_NAME=Lua-cURLv3-${LIB_VERSION}
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

if [ ! -d ${LIB_INSTALL_DIR} ]; then
	cmake -S ${LIB_SRC_DIR} \
		-B ${LIB_BUILD_DIR} \
		-DCMAKE_BUILD_TYPE=Release \
		-DBUILD_SHARED_LIBS=OFF \
		-DLUA_INSTALL_DIRECTORY=${LUA_ROOT} \
		-DCURL_INSTALL_DIRECTORY=${CURL_ROOT} \
		-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} \
		-DCMAKE_INSTALL_PREFIX=${LIB_INSTALL_DIR} \
		-DANDROID_ABI=${ANDROID_ABI} \
		-DANDROID_PLATFORM=${ANDROID_PLATFORM}
	cmake --build ${LIB_BUILD_DIR}
	cmake --install ${LIB_BUILD_DIR}
fi

# =================================Install md5===================================
LIB_VERSION=1.3
LIB_NAME=md5-${LIB_VERSION}
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

if [ ! -d ${LIB_INSTALL_DIR} ]; then
	cmake -S ${LIB_SRC_DIR} \
		-B ${LIB_BUILD_DIR} \
		-DCMAKE_BUILD_TYPE=Release \
		-DBUILD_SHARED_LIBS=OFF \
		-DLUA_INSTALL_DIRECTORY=${LUA_ROOT} \
		-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} \
		-DCMAKE_INSTALL_PREFIX=${LIB_INSTALL_DIR} \
		-DANDROID_ABI=${ANDROID_ABI} \
		-DANDROID_PLATFORM=${ANDROID_PLATFORM}
	cmake --build ${LIB_BUILD_DIR}
	cmake --install ${LIB_BUILD_DIR}
fi
